package com.qingzhu.dao.impl;

import com.github.pagehelper.Page;
import com.qingzhu.dao.GoodsDao;
import com.qingzhu.pojo.Goods;
import com.qingzhu.util.CreateSqlSessionFactory;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class GoodsDaoImpl implements GoodsDao {

    @Override
    public Goods findGoodsById(int goodsId,boolean excludeDel) {
        try(SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)){
            GoodsDao mapper = sqlSession.getMapper(GoodsDao.class);
            return mapper.findGoodsById(goodsId,excludeDel);
        }
    }

    @Override
    public List<Goods> findGoodsByBrandId(int brandId) {
        try(SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)){
            GoodsDao mapper = sqlSession.getMapper(GoodsDao.class);
            return mapper.findGoodsByBrandId(brandId);
        }
    }

    @Override
    public Page<Goods> findGoodsByLike(String keyword, int pageNum, int pageSize,boolean excludeDel) {
        try(SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)){
            GoodsDao mapper = sqlSession.getMapper(GoodsDao.class);
            return mapper.findGoodsByLike(keyword,pageNum,pageSize,excludeDel);
        }
    }

    @Override
    public List<Goods> findGoodsFromAttribute(String attributeId) {
        try(SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)){
            GoodsDao mapper = sqlSession.getMapper(GoodsDao.class);
            return mapper.findGoodsFromAttribute(attributeId);
        }
    }

    @Override
    public int addGoods(Goods goods) {
        try(SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)){
            GoodsDao mapper = sqlSession.getMapper(GoodsDao.class);
            return mapper.addGoods(goods);
        }
    }

    @Override
    public int updateGoods(Goods goods) {
        try(SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)){
            GoodsDao mapper = sqlSession.getMapper(GoodsDao.class);
            return mapper.updateGoods(goods);
        }
    }

    @Override
    public int updateGoodsState(String state, String goodsId) {
        try(SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)){
            GoodsDao mapper = sqlSession.getMapper(GoodsDao.class);
            return mapper.updateGoodsState(state,goodsId);
        }
    }
}
